home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Chans / fax / ps250 / ps250_basic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  3.6 KB  |  208 lines

  1. /* ps250_basic.c: basic encode decode fax control sequences  */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Chans/fax/ps250/RCS/ps250_basic.c,v 6.0 1991/12/18 20:07:26 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Chans/fax/ps250/RCS/ps250_basic.c,v 6.0 1991/12/18 20:07:26 jpo Rel $
  9.  *
  10.  * $Log: ps250_basic.c,v $
  11.  * Revision 6.0  1991/12/18  20:07:26  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17. #include "util.h"
  18. #include "ps250.h"
  19. #ifdef SYS5        /* Can't use isode/sys.file.h due to termios.h */
  20. #include <fcntl.h>
  21. #else
  22. #include <sys/file.h>
  23. #endif
  24. #include <termios.h>
  25. #include <fcntl.h>
  26. #include <stdio.h>
  27. #include <signal.h>
  28.  
  29. extern void advise (), adios ();
  30. int fax_debug = 0;
  31.  
  32. static void hexdump();
  33.  
  34. /* #define CHECKSUM */
  35.  
  36. /*
  37.  * basic io (chunk level)
  38.  */
  39.  
  40. #define    INC     32
  41.  
  42. int    writex (fd, buffer, size)
  43. int    fd;
  44. char    *buffer;
  45. int    size;
  46. {
  47.     char    *cp = buffer;
  48.     int    done = cp - buffer;
  49.     int    num, n;
  50.     while (done < size) {
  51.         if (size - done < INC)
  52.             num = size - done;
  53.         else
  54.             num = INC;
  55.  
  56.         if ((n = write (fd, cp, num)) <= 0)
  57.             return NOTOK;
  58. /*        if (fax_debug)
  59.             hexdump ("<=", cp, n);*/
  60.         cp += num;
  61.         done = cp - buffer;
  62.     }
  63.     return done;
  64. }
  65.         
  66. static int readx (fd, buffer, size)
  67. int    fd;
  68. char    *buffer;
  69. int    size;
  70. {
  71.     char    *cp = buffer;
  72.     int    count = 0, n;
  73.  
  74.     while (size > 0) {
  75.         if ((n = read (fd, cp, size)) <= 0)
  76.             return NOTOK;
  77. /*        if (fax_debug)
  78.             hexdump ("=>", cp, n);*/
  79.         count += n;
  80.         size -= n;
  81.         cp += n;
  82.     }
  83.     return count;
  84. }
  85.  
  86. /*   
  87.  * basic io (packet level)
  88.  */
  89.  
  90. int fax_writepacket (fd, fp)
  91. Faxcomm *fp;
  92. {
  93.     char    *cp;
  94.     int    len;
  95.     char    buffer[FAXBUFSIZ];
  96.  
  97.     cp = buffer;
  98.     *cp ++ = FAX_HEADER;
  99.     *cp ++ = FAX_HEADER;
  100.     *cp ++ = FAX_CONTROL;
  101.     *cp ++ = FAX_ADDRESS;
  102.     len = 1 + fp -> len;
  103.     *cp ++ = len == 256 ? 0 : len;
  104.     *cp ++ = fp -> command;
  105.     if (fp -> len) {
  106.         bcopy (fp -> data, cp, fp -> len);
  107.         cp += fp -> len;
  108.     }
  109.     *cp = fax_checksum (buffer, cp - buffer);
  110.     cp ++;
  111.  
  112.     if (writex (fd, buffer, cp - buffer) != cp - buffer)
  113.         return NOTOK;
  114.     return OK;
  115. }
  116.  
  117. int    fax_readpacket (fd, fp)
  118. int    fd;
  119. Faxcomm *fp;
  120. {
  121.     char buffer[FAXBUFSIZ];
  122.     unsigned int    len;
  123.  
  124.     if ((len = readx (fd, buffer, FAXHDRSIZE)) != FAXHDRSIZE) {
  125.         advise ("read", "Read fax header size failed");
  126.         return NOTOK;
  127.     }
  128.     if (buffer[0] != FAX_HEADER ||
  129.         buffer[1] != FAX_HEADER ||
  130.         buffer[2] != FAX_CONTROL ||
  131.         buffer[3] != FAX_ADDRESS) {
  132.         advise (NULLCP, "Incorrect Fax header");
  133.         return NOTOK;
  134.     }
  135.     len = (unsigned char) buffer[FAXHDRSIZE - 1];
  136.     if (len == 0)
  137.         len = 256;
  138.  
  139. #ifdef CHECKSUM
  140.     len ++;
  141. #endif
  142.     if (readx (fd, &buffer[FAXHDRSIZE], len) != len) {
  143.         advise ("read", "failed to read %d more bytes", len);
  144.         return NOTOK;
  145.     }
  146. #ifdef CHECKSUM
  147.     if ((tmp = fax_checksum (buffer, FAXHDRSIZE + len)) !=
  148.         buffer[FAXHDRSIZE + len - 1]) {
  149.         advise (NULLCP, "Checksum failed %d != %d",
  150.             buffer[FAXHDRSIZE + len - 1], tmp);
  151.         return NOTOK;
  152.     }
  153. #endif
  154.     fp -> flags = 0;
  155.     fp -> command = buffer[FAXHDRSIZE];
  156.     fp -> len = len - 1;
  157.     if (fp -> len)
  158.         bcopy (&buffer[FAXHDRSIZE+1], fp -> data, fp -> len);
  159.     return OK;
  160. }
  161.  
  162. /* 
  163.  * checksum routine
  164.  */
  165.  
  166. int fax_checksum (bp, len)
  167. int    len;
  168. char    *bp;
  169. {
  170.     char    *cp, *ep = bp + len;
  171.     int sum = 0;
  172.  
  173.     for (cp = bp + 2; cp < ep; cp ++)
  174.         sum += *cp;
  175.     return 0x100 - (sum % 0x100);
  176. }
  177.  
  178. /* misc */
  179.  
  180. static void hexdump (str, bp, n)
  181. char    *str;
  182. char    *bp;
  183. int    n;
  184. {
  185.     static char hex[] = "0123456789ABCDEF";
  186.  
  187.     fputs (str, stdout);
  188.     while (n -- > 0) {
  189.         putchar (hex[(*bp >> 4) & 0xf]);
  190.         putchar (hex[*bp & 0xf]);
  191.         bp ++;
  192.     }
  193.     putchar ('\n');
  194. }
  195.  
  196. int fax_simplecom (fd, type)
  197. int     fd;
  198. int     type;
  199. {
  200.         Faxcomm pack;
  201.  
  202.         pack.flags = 0;
  203.         pack.command = type;
  204.         pack.len = 0;
  205.  
  206.         return fax_writepacket (fd, &pack);
  207. }
  208.